package org.whuims.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.whuims.entity.QuotaContext;
import org.whuims.entity.QuotaStandardContext;
import org.whuims.entity.SampleQuotas;
import org.whuims.entity.Standard;
import org.whuims.service.standard.StandardService;

import javax.servlet.ServletRequest;
import java.util.*;

/**
 * Created by Roger on 2016/12/28.
 */
@Controller
@RequestMapping("/quota")
public class QuotaController {

    @Autowired
    private StandardService standardService;

    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model, ServletRequest request) {
        model.addAttribute("stdPage", standardService.getStandardList(null, 1, 10, null));
        return "test";
    }

    @RequestMapping(value = "search", method = RequestMethod.GET)
    public String searchEntry() {
        return "std/quota_search";
    }

    @RequestMapping(value = "search", method = RequestMethod.POST)
    public String searchEntry(@RequestParam(required = false, defaultValue = "1") Integer pageNumber,
                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                              String keyword,
                              Model model) {
        model.addAttribute("keyword", keyword);
        model.addAttribute("stdPage", standardService.getStandardListByQuota(keyword, pageNumber, pageSize, null));
        return "std/quota_search_result";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Page<Standard> page(@RequestParam(required = false, defaultValue = "1") Integer pageNumber,
                               @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                               String keyword) {
        return standardService.getStandardListByQuota(keyword, pageNumber, pageSize, null);
    }

    @RequestMapping(value = "/push", method = RequestMethod.POST)
    public String submit(String selectId, Model model) {
        List<Standard> list = new ArrayList<Standard>();
        for (String id : selectId.split(",")) {
            list.add(standardService.getStandard(Long.parseLong(id)));
        }
        model.addAttribute("stdIdList", selectId);
        model.addAttribute("stdList", list);
        return "std/submit_list";
    }

    @RequestMapping(value = "/clusterView", method = {RequestMethod.POST, RequestMethod.GET})
    public String clusterView(String stdIdList, Model model) {
        List<Standard> list = new ArrayList<Standard>();
        for (String stdId : stdIdList.split(",")) {
            list.add(standardService.getStandard(Long.parseLong(stdId)));
        }
        model.addAttribute("stdList", list);
        model.addAttribute("stdIdList", stdIdList);

        Map<String, Integer> quotaMap = new HashMap<String, Integer>();
        Map<String, List<QuotaStandardContext>> quotaContextsMap = new HashMap<String, List<QuotaStandardContext>>();
        for (Standard std : list) {
            Set<String> quotaStrList = standardService.detectQuotaStrs(std);
            for (String str : quotaStrList) {
                if (!quotaMap.containsKey(str)) {
                    quotaMap.put(str, quotaMap.size());
                    quotaContextsMap.put(str, new ArrayList<QuotaStandardContext>());
                }
            }
            List<QuotaContext> quotaContextList = standardService.detectQuotaContexts(std, quotaStrList);
            for (QuotaContext qc : quotaContextList) {
                String qStr = qc.getQuota();
                List<String> contexts = qc.getContexts();
                for (String context : contexts) {
                    QuotaStandardContext qs = new QuotaStandardContext();
                    qs.setQuota(qStr);
                    qs.setQuotaContext(context);
                    qs.setStd(std);
                    quotaContextsMap.get(qStr).add(qs);
                }
            }
        }
        List<SampleQuotas> samples = new ArrayList<SampleQuotas>();
        for (String str : quotaMap.keySet()) {
            int id = quotaMap.get(str);
            List<QuotaStandardContext> qss = quotaContextsMap.get(str);
            SampleQuotas quotas = new SampleQuotas();
            quotas.setId(id);
            quotas.setQuota(str);
            quotas.setContexts(qss);
            if (qss.size() > 0)
                samples.add(quotas);
        }
        model.addAttribute("samples", samples);
        return "std/cluster_view";
    }
}
